﻿using BP.DA;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace BP.Sys
{
    /// <summary>
    /// 存储
    /// </summary>
    public enum DataPackerStorage
    {
        /// <summary>
        /// 所有的用户
        /// </summary>
        ByAllUser,
        /// <summary>
        /// 按照用户存储
        /// </summary>
        ByUser,
        /// <summary>
        /// 按照部门存储
        /// </summary>
        ByDept,
        /// <summary>
        /// 按照组织存储
        /// </summary>
        ByOrg
    }
    public enum DataPackerReadTime
    {
        /// <summary>
        /// 每次都要读取
        /// </summary>
        EveyTime,
        /// <summary>
        /// 按照版本号
        /// </summary>
        ByVer,
    }
    abstract public class DataPacketBase
    {
        #region 属性.
        public string Title = "";
        public Hashtable ht = new Hashtable();
        public Hashtable paras = new Hashtable();
        public DataSet ds = null;
        #endregion 属性.

        #region 获得数据.
        public string GetValString(string key)
        {
            DataTable dt = this.ds.Tables[key];

            if (dt.Rows.Count != 1)
                throw new Exception("err@数据表应该是一行一列,数据不是一行,目前有行:" + dt.Rows.Count + "条.");

            if (dt.Columns.Count != 1)
                throw new Exception("err@数据表应该是一行一列,数据不是一列,目前有列:" + dt.Columns.Count + "列.");


            return dt.Rows[0][0].ToString();
        }
        public int GetValInt(string key)
        {
            return int.Parse(this.GetValString(key));
        }
        public float GetValFloat(string key)
        {
            return float.Parse(this.GetValString(key));
        }
        public DataTable GetDataTable(string key)
        {
            return this.ds.Tables[key];
        }
        #endregion 获得数据.

        public DataPacketBase()
        {
        }
        /// <summary>
        /// 增加数据源
        /// </summary>
        /// <param name="mark">标记</param>
        /// <param name="note">说明</param>
        /// <param name="sql">SQL表达式</param>
        public void AddSQL(string mark, string note, string sql)
        {
            this.ht.Add(mark, sql);
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        abstract public void Init();

        //获得数据
        public DataSet GenerData()
        {
            this.ds = new DataSet();

            foreach (string key in this.ht.Keys)
            {
                string sql = this.ht[key].ToString();
             //   sql = Glo.DealExp(sql, null, "");
                DataTable dt = DBAccess.RunSQLReturnTable(sql);
                dt.TableName = key;
                ds.Tables.Add(dt);
            }
            return this.ds;
        }

    }
}
